﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MD8800.util
{
    class cpu
    {

        private PerformanceCounter cpuCounter;
        private PerformanceCounter ramCounter;

        public cpu()
        {
            cpuCounter = new PerformanceCounter();

            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
        }

        #region /*Methoden*/
        /// <summary>
        /// gibt die jetzige CPU Benutzung aus
        /// </summary>
        /// <returns>string, CPU in %</returns>
        public string getCurrentCpuUsage()
        {
            int x = Convert.ToInt32(cpuCounter.NextValue());
            return x + "%";
        }

        /// <summary>
        /// gibt die jetzige RAM nutzung aus
        /// </summary>
        /// <returns>string, Ram in GB</returns>
        public string getAvailableRAM()
        {
            float x = ramCounter.NextValue();/// 1000;
            String s = "" + x.ToString().Substring(0, 1) + "." + x.ToString().Substring(1, 1);
            return s + "GB";
        }

        /// <summary>
        ///  gibt den cpu string im format: [---  ]
        /// </summary>
        /// <returns>string</returns>
        public string getCPUstring()
        {
            String s = "CPU:[";
            int x = Convert.ToInt32(cpuCounter.NextValue());
            if (x <= 1) x = 1;
            x = x / 10;
            for (int i = 0; i < x; i++)
            {
                s += "-";
            }
            for (int i = x; i < 10; i++)
            {
                s += " ";
            }
            s += "]";
            return s;
        }
        /// <summary>
        /// gibt den Ram string im format: [---  ]
        /// </summary>
        /// <returns>string</returns>
        public string getRAMstring()
        {
            String s = "RAM:[";
            int x = 10 - Convert.ToInt32(ramCounter.NextValue() / 1000 * 3.4);

            for (int i = 0; i < x; i++)
            {
                s += "-";
            }
            for (int i = x; i < 10; i++)
            {
                s += " ";
            }
            s += "]";
            return s;
        }
        /// <summary>
        /// Beschreibt das Display mit CPU und Ram werten
        /// </summary>
        /// <param name="port">der offene Port</param>
        public void writeCPUandRam(PortController port)
        {
            port.write(opcodes.ROW_FIRST);
            port.write(opcodes.CLEAR_TEXT);
            port.write(getCPUstring());
            port.write(opcodes.ROW_SECOND);
            port.write(opcodes.CLEAR_TEXT);
            port.write(getRAMstring());
        }
        #endregion

    }
}
